//
//  Int+Extension.swift
//  PassVPN
//
//  Created by yiwanjun on 2018/1/2.
//  Copyright © 2018年 Asteroid. All rights reserved.
//

import Foundation

/// 时间格式
public enum TimeformatType {
    /// hh
    case second
    /// hh:mm
    case minute
    /// hh:mm:ss
    case hour
}

public extension Int {
    
    /// 格式化为时间格式，例如：28391.timeformat() -> 07:53:11
    /// - Parameter type: 时间格式
    /// - Returns: 时分秒的字符串
    func timeformat(type: TimeformatType = .hour) -> String {
        //format of hour
        let hour = String(format: "%02d", self/3600)
        
        //format of minute
        let minute = String(format: "%02d", (self%3600)/60)
        
        //format of second
        let second = String(format: "%02d", self%60)
        
        //format of time
        
        var time = ""
        switch type {
        case .hour:
            time = String(format: "%@:%@:%@", hour, minute, second)
        case .minute:
            time = String(format: "%@:%@", minute, second)
        case .second:
            time = String(format: "%@", second)
        }
        
        return time;
    }

    /// 加1但是不能大于某个数字
    /// - Parameter num: 不能被大于的数字
    /// - Returns: 加一后的数字
    func plusOne(num:Int) -> Int {
        var a = self
        a += 1
        return a > num ? num : a
    }
    
    /// 判断是否等于0，等于0时返回传入参数，否则返回自身
    /// - Parameter num: 参数
    /// - Returns: 结果
    func equal0(num:Int) -> Int {
        return self == 0 ? num : self
    }
    
    /// 根据 format 转化成 DateTimeSring
    /// - Parameter format: 格式
    /// - Returns: DateTimeSring
    func dateString(_ format: String = "yyyy-MM-dd HH:mm") -> String{
        let date = Date(timeIntervalSince1970: TimeInterval(self))
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: date)
    }
    
    /// 转化成bool值
    /// - Returns: bool值
    func boolValue() -> Bool {
        if self == 0 {
            return false
        }else{
            return true
        }
    }
}

public extension Bool {
    
    /// 取反
    var reversed: Bool {
        !self
    }
}
